package com.example.demo.filter;

import com.example.demo.utils.IpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Component
@WebFilter(urlPatterns = "/*", filterName = "IPAddressFilter")
class TestFilter implements Filter {

    private static final Logger LOG = LoggerFactory.getLogger(Filter.class);

    @Override
    public void init(FilterConfig arg0) {
        LOG.info("过滤器初始化");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        LOG.info(((HttpServletRequest) servletRequest).getRequestURI());
        LOG.info(IpUtil.getIpAddr((HttpServletRequest) servletRequest));
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        LOG.info("过滤器销毁了");
    }

}
